package proTrack.model;

import java.security.Principal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.security.auth.Subject;

@Entity
@NamedQueries({
	@NamedQuery(name = "AllAccounts", query = "SELECT acc FROM Account acc"),
    @NamedQuery(name = "DeleteAccount", query = "DELETE FROM Account a WHERE a.name = ?1")
                            
    })
public class Account implements Principal {

    @Id
    private String name;

    @Column(unique=true, nullable=false)
    private String password;

    public Account() {

    }

    public Account(final String name, final String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }
    
    @Override
    public String toString() {
    	return "Account " + getName() + " " + getPassword();
    }

	public static Account getAccount(final Subject subject) {
		for (Principal p : subject.getPrincipals()) {
			if (p instanceof Account) {
				return (Account) p;
			}
		}
		return null;
	}


}
